package com.example.rabbitdemo.exchanger.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 1. 配置类
@Configuration
public class TopicExchangeConfig {
    
    // 声明主题交换机
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("exchange.topic", true, false);
    }
    
    // 声明队列
    @Bean
    public Queue usaQueue() {
        return new Queue("queue.usa.all");
    }
    
    @Bean
    public Queue newsQueue() {
        return new Queue("queue.news.global");
    }
    
    @Bean
    public Queue weatherQueue() {
        return new Queue("queue.usa.weather");
    }
    
    // 使用通配符进行绑定
    // `usa.#` 匹配以 "usa." 开头的所有路由键，如 "usa.news", "usa.weather"
    @Bean
    public Binding usaBinding(TopicExchange topicExchange, Queue usaQueue) {
        return BindingBuilder.bind(usaQueue).to(topicExchange).with("usa.#");
    }
    
    // `#.news` 匹配以 ".news" 结尾的所有路由键，如 "usa.news", "europe.news"
    @Bean
    public Binding newsBinding(TopicExchange topicExchange, Queue newsQueue) {
        return BindingBuilder.bind(newsQueue).to(topicExchange).with("#.news");
    }
    
    // `usa.weather` 精确匹配该路由键
    @Bean
    public Binding weatherBinding(TopicExchange topicExchange, Queue weatherQueue) {
        return BindingBuilder.bind(weatherQueue).to(topicExchange).with("usa.weather");
    }
}



